package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.EnginHierCo;
import com.engineering.elink.co.HierCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.EnginHomeView;
import com.engineering.elink.hierdao.HierStepMapper;
import com.engineering.elink.hierdao.PlannHierMapper;
import com.engineering.elink.service.EnginHomeService;
import com.engineering.elink.skilldao.SkillFileMapper;
import com.engineering.elink.skilldao.SkillMapper;
import com.engineering.elink.util.CollectionUtil;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.CourseMapper;
import com.engineering.elink.videodao.EnginHomeViewMapper;
import com.engineering.elink.videodao.ExclusiveMapper;
import com.engineering.elink.vo.EnginBrowseVo;
import com.engineering.elink.vo.EnginCourseVo;
import com.engineering.elink.vo.EnginNewsVo;
import com.engineering.elink.vo.EnginPostBarVo;
import com.engineering.elink.vo.PlannHierCountVo;
import com.engineering.elink.vo.SkillFileVo;

@Service
public class EnginHomeServiceImpl implements EnginHomeService{

	@Autowired
	private ExclusiveMapper exclusiveMapper;
	
	@Autowired
	private PlannHierMapper plannHierMapper;
	
	@Autowired
	private HierStepMapper hierStepMapper;
	
	@Autowired
	private SkillMapper skillMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Autowired
	private SkillFileMapper skillFileMapper;
	
	@Autowired
	private EnginHomeViewMapper enginHomeViewMapper;
	
	@Override
	public Map<String, Object> getExcluAll() {
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, exclusiveMapper.selectExcluAll());
	}

	@Override
	public Map<String, Object> getProfePlann(EnginHierCo co, String userId) {
		PlannHierCountVo vo = plannHierMapper.selectInfo(co.getHierId());
		if (vo != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("plannHier", vo);
			//体系进阶信息
			map.put("hierStepList", hierStepMapper.getStepList("1,2"));
			
			HierCo hCo=new HierCo();
			hCo.setHierId(co.getHierId());
			hCo.setGrade(co.getGrade());
			hCo.setPageSize(co.getCount());
			
			//工程技能
			map.put("skillList", skillMapper.getHierSkill(hCo));
			map.put("skillPagination", new Pagination(hCo, skillMapper.selectHierSkillCount(hCo)));
			
			//工程视频
			map.put("courseList", courseMapper.getHierCourse(hCo));
			map.put("coursePagination", new Pagination(hCo, courseMapper.selectHierCourseCount(hCo)));
			
			//工程资料
			map.put("fileList", plannHierMapper.selectHierFile(hCo));
			map.put("filePagination", new Pagination(hCo, plannHierMapper.selectHierFileCount(hCo)));
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
		} else {
			return ResJsonUtil.getMap(ErrorType.PLANNHIER_ERROR);
		}
	}

	@Override
	public Map<String, Object> getEnginFile(Integer scId, Integer dcId, String userId,Integer count) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("scId", scId);
		map.put("dcId", dcId);
		map.put("count", count+1);
		map.put("mold", 0);
		map.put("tcId", 1);
		
		List<SkillFileVo> voList=plannHierMapper.selectEnginHomeFile(map);
		
		map.put("count", count);
		map.put("mold", 0);
		map.put("tcId", 2);
		List<SkillFileVo> rightList=plannHierMapper.selectEnginHomeFile(map);
		
		if(CollectionUtil.collectionIsNotEmpty(voList)){
			map.clear();
			
			map.put("topFile", voList.get(0));
			
			voList.remove(0);
			
			map.put("leftFileList", voList);
			
			map.put("rightFileList", rightList);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
		}
		
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> getEnginCourse(Integer scId, Integer dcId, String userId,Integer count) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("scId", scId);
		map.put("thirdId", dcId);
		map.put("count", count);
		
		List<EnginCourseVo> voList=courseMapper.selectEnginCourse(map);
		if(CollectionUtil.collectionIsNotEmpty(voList)){
			map.clear();
			map.put("courseList", voList);
			return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
		}
		
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> getEnginLibrary(Integer scId, Integer dcId, String userId,Integer count) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("scId", scId);
		map.put("scId", scId);
		map.put("count", count);
		List<EnginHomeView> viewList=enginHomeViewMapper.selectEnginlibrary(map);
		if(CollectionUtil.collectionIsNotEmpty(viewList)){
			map.clear();
			map.put("libraryList", viewList);
			return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> getEnginPostBar(Integer scId, Integer dcId, Integer hierId, Integer count, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("scId", scId);
		map.put("dcId", dcId);
		map.put("hierId", hierId);
		map.put("count", count);
		
		List<EnginPostBarVo> postbarCateList=plannHierMapper.selectPostBarByCatetory(map);
		List<EnginPostBarVo> postbarProfeList=plannHierMapper.selectPostBarByProfe(map);
		
		map.clear();
			
		map.put("postbarCateList", postbarCateList);
		map.put("postbarProfeList", postbarProfeList);
		
		map.put("postbarCount", plannHierMapper.selectEnginPostBarCount()); 
			
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
		
	}

	@Override
	public Map<String, Object> getEnginNews(String userId) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("typeId", 50);
		map.put("count", 4);
		List<EnginNewsVo> newsVoList=enginHomeViewMapper.selectNewsByInside(map);
		map.clear();
		map.put("typeId1", 49);
		map.put("count1", 2);
		map.put("typeId2", 51);
		map.put("count2", 2);
		map.put("typeId3", 52);
		map.put("count3", 2);
		List<EnginNewsVo> voList=enginHomeViewMapper.selectNewsByTypeId(map);
		map.clear();
		if(CollectionUtil.collectionIsNotEmpty(newsVoList)){
			map.put("topNews", newsVoList.get(0));
			newsVoList.remove(0);
			map.put("leftNewsList", newsVoList);
		}
		map.put("rightNewsList", voList);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
	}

	@Override
	public Map<String, Object> getEnginBrowse(String userId, Integer count) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("count", count);
		List<EnginBrowseVo> list=enginHomeViewMapper.selectHomeBrowseByUserId(map);
		map.clear();
		map.put("enginBrowseList", list);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
	}
	
	
}
